Refactoring. Added documentation to most of the database functions.