Some refactoring and code cleaning.