Full refactoring and clean up