restructured some of the database queries