simplifying and refactoring app.js code