refactored controller code
fixed bug in the controller'