Reorganized database code and added comments for each function