Refactored app.js code to cache variables