clean up the code and now using a class for database operations