Refactors db handling and routes