Finish refactoring existing functions from database.js