complete refactoring and database connection