Started refactoring.
Fixed merge issues.