Refactoring of the database code, removed dead code, other goodies