Refactored all controller code to simplify it