Cleanup and restructure controller code