Code cleanup, some helper methods and minor refactoring.