Refactoring and some code cleanup.