Improved database module

Added more features, fixed some bugs and added more test cases.