refactor main loop into controller and fix compiler warnings