Refactored code & improved db wrapper