app.js refactored slightly