Added helper function and cleaned up controller