Cleaned up version.

Cleaned up and split into different classes and methods.