diff --git a/ruby/rbwritedeclaration.cpp b/ruby/rbwritedeclaration.cpp
index 4f57328..3b792ed 100644
--- a/ruby/rbwritedeclaration.cpp
+++ b/ruby/rbwritedeclaration.cpp
@@ -51,6 +51,7 @@
 #include "databaseinfo.h"
 #include "customwidgetsinfo.h"

+#include <QFileInfo>
 #include <QTextStream>

 namespace Ruby {
@@ -65,7 +66,9 @@ WriteDeclaration::WriteDeclaration(Uic *uic)  :

 void WriteDeclaration::acceptUI(DomUI *node)
 {
-    QString qualifiedClassName = node->elementClass() + m_option.postfix;
+    //QString qualifiedClassName = node->elementClass() + m_option.postfix;
+    QString qualifiedPostfix = m_option.postfix.mid(0, 1).toUpper() + m_option.postfix.mid(1);
+    QString qualifiedClassName = QFileInfo(m_option.inputFile).baseName() + qualifiedPostfix;
     // Ruby classnames must start with an upper case letter
     QString className = qualifiedClassName.mid(0, 1).toUpper() + qualifiedClassName.mid(1);

@@ -82,17 +85,18 @@ void WriteDeclaration::acceptUI(DomUI *node)
         namespaceList.removeLast();
     }

-    QListIterator<QString> it(namespaceList);
+    /*QListIterator<QString> it(namespaceList);
     while (it.hasNext()) {
         QString ns = it.next();
         if (ns.isEmpty())
             continue;
-    }
+    }*/

     if (namespaceList.count())
         m_output << "\n";

-    m_output << "class " << m_option.prefix << className << "\n";
+    m_output << "module Ui\n";
+    m_output << m_option.indent << "class " << className << " < Qt::Object\n";

     const QStringList connections = m_uic->databaseInfo()->connections();
     for (int i=0; i<connections.size(); ++i) {
@@ -101,7 +105,7 @@ void WriteDeclaration::acceptUI(DomUI *node)
         if (connection == QLatin1String("(default)"))
             continue;

-        m_output << m_option.indent << "@" << connection << "Connection = Qt::SqlDatabase.new\n";
+        m_output << m_option.indent << m_option.indent  << "@" << connection << "Connection = Qt::SqlDatabase.new\n";
     }

     TreeWalker::acceptWidget(node->elementWidget());
@@ -120,9 +124,10 @@ void WriteDeclaration::acceptUI(DomUI *node)
         WriteIconInitialization(m_uic).acceptUI(node);
     }

+    m_output << m_option.indent << "end\n";
     m_output << "end\n\n";

-    it.toBack();
+    /*it.toBack();
     while (it.hasPrevious()) {
         QString ns = it.previous();
         if (ns.isEmpty())
@@ -132,33 +137,60 @@ void WriteDeclaration::acceptUI(DomUI *node)
     if (namespaceList.count())
         m_output << "\n";

-    if (m_option.generateNamespace && !m_option.prefix.isEmpty()) {
-        namespaceList.append(QLatin1String("Ui"));
+    namespaceList.append(QLatin1String("Ui"));*/

-        QListIterator<QString> it(namespaceList);
-        while (it.hasNext()) {
-            QString ns = it.next();
-            if (ns.isEmpty())
-                continue;
+    int intend_level = 0;
+    QListIterator<QString> it(namespaceList);
+    while (it.hasNext()) {
+        QString ns = it.next();
+        if (ns.isEmpty())
+            continue;

-            m_output << "module " << ns.mid(0, 1).toUpper() << ns.mid(1) << "\n";
+        for(int i=0; i<intend_level;i++) {
+            m_output << m_option.indent;
         }
+        m_output << "module " << ns.mid(0, 1).toUpper() << ns.mid(1) << "\n";
+        intend_level++;
+    }

-        m_output << m_option.indent << "class "  << className << " < " << m_option.prefix << className << "\n";
-        m_output << m_option.indent << "end\n";
+    QString currentIntend = "";
+    for(int j=0; j<intend_level;j++) {
+        currentIntend += m_option.indent;
+    }

-        it.toBack();
-        while (it.hasPrevious()) {
-            QString ns = it.previous();
-            if (ns.isEmpty())
-                continue;
+    DomWidget*  parentWidget = node->elementWidget();
+    QString parentClass = parentWidget->attributeClass();
+    parentClass.replace(QRegExp("^Q"), "Qt::");
+    parentClass.replace(QRegExp("^K"), "KDE::");
+    m_output << currentIntend << "class "  << className << " < "  << parentClass << "\n";
+    m_output << currentIntend << m_option.indent << "attr_reader :ui\n\n";
+    m_output << currentIntend << m_option.indent << "def initialize(parent=nil)\n";
+    m_output << currentIntend << m_option.indent << m_option.indent << "@ui = Ui::" << className << ".new\n";
+    m_output << currentIntend << m_option.indent << m_option.indent << "super\n";
+    m_output << currentIntend << m_option.indent << m_option.indent << "@ui.setup_ui(self)\n";
+    m_output << currentIntend << m_option.indent << m_option.indent << "ObjectSpace.define_finalizer(self, " << className << ".delete_ui(@ui))\n";
+    m_output << currentIntend << m_option.indent << "end\n\n";
+    m_output << currentIntend << m_option.indent << "private\n";
+    m_output << currentIntend << m_option.indent << "def " << className << ".delete_ui(ui_to_delete)\n";
+    m_output << currentIntend << m_option.indent << m_option.indent << "proc{ui_to_delete.dispose unless ui_to_delete.disposed?}\n";
+    m_output << currentIntend << m_option.indent << "end\n";
+    m_output << currentIntend << "end\n";

-            m_output << "end  # module " << ns << "\n";
-        }
+    it.toBack();
+    while (it.hasPrevious()) {
+        QString ns = it.previous();
+        if (ns.isEmpty())
+            continue;

-        if (namespaceList.count())
-            m_output << "\n";
+        intend_level--;
+        for(int k=0; k<intend_level;k++) {
+            m_output << m_option.indent;
+        }
+        m_output << "end  # module " << ns << "\n";
     }
+
+    //if (namespaceList.count())
+    m_output << "\n";
 }

 void WriteDeclaration::acceptWidget(DomWidget *node)
@@ -169,7 +201,7 @@ void WriteDeclaration::acceptWidget(DomWidget *node)

 	QString item = m_driver->findOrInsertWidget(node);
 	item = item.mid(0, 1).toLower() + item.mid(1);
-    m_output << m_option.indent << "attr_reader :" << item << "\n";
+    m_output << m_option.indent << m_option.indent << "attr_reader :" << item << "\n";

     TreeWalker::acceptWidget(node);
 }
@@ -182,7 +214,7 @@ void WriteDeclaration::acceptLayout(DomLayout *node)

 	QString item = m_driver->findOrInsertLayout(node);
 	item = item.mid(0, 1).toLower() + item.mid(1);
-    m_output << m_option.indent << "attr_reader :" << item << "\n";
+    m_output << m_option.indent << m_option.indent << "attr_reader :" << item << "\n";

     TreeWalker::acceptLayout(node);
 }
@@ -191,7 +223,7 @@ void WriteDeclaration::acceptSpacer(DomSpacer *node)
 {
 	QString item = m_driver->findOrInsertSpacer(node);
 	item = item.mid(0, 1).toLower() + item.mid(1);
-    m_output << m_option.indent << "attr_reader :" << item << "\n";
+    m_output << m_option.indent << m_option.indent << "attr_reader :" << item << "\n";

     TreeWalker::acceptSpacer(node);
 }
@@ -200,7 +232,7 @@ void WriteDeclaration::acceptActionGroup(DomActionGroup *node)
 {
 	QString item = m_driver->findOrInsertActionGroup(node);
 	item = item.mid(0, 1).toLower() + item.mid(1);
-    m_output << m_option.indent << "attr_reader :" << item << "\n";
+    m_output << m_option.indent << m_option.indent << "attr_reader :" << item << "\n";

     TreeWalker::acceptActionGroup(node);
 }
@@ -209,7 +241,7 @@ void WriteDeclaration::acceptAction(DomAction *node)
 {
 	QString item = m_driver->findOrInsertAction(node);
 	item = item.mid(0, 1).toLower() + item.mid(1);
-    m_output << m_option.indent << "attr_reader :" << item << "\n";
+    m_output << m_option.indent << m_option.indent << "attr_reader :" << item << "\n";

     TreeWalker::acceptAction(node);
 }
