Refactored a bit so that multiple database can be accessed