Fixed db issues and refactored structure