Refactored and documented controller.js