refactored code for new database structure