Started to refactor code from direct port to clean code.