Refactored code added some DB functionality and added comments