reorganized app.js code