Some refactoring and cleaning up

Some refactoring and cleaning up