Code cleanup

Code clean up and refactoring.