Fixed some bugs in the controller; some optimizations