Major refactoring

- Major refactoring to make code easier to test and more extensible.
- Added UML diagram