added test case for controller.js and fixed some bugs and cleaned the code for controller.js