code cleanup and refactoring.
validation tests ok