Initial code refactoring and cleaning.