restructure code, cleanup main controller