A bunch of changes, refactoring and clean up