a lot of refactoring, database issue solved