refactoring controller setup
a lot more refactoring and a bit of bug fixing