Refactors DB logic out of app.rb and maybe resolves a DB bug