Changed implementation of controller to improve clarity.