Code rewrite with classes

Easier for mocking and better for new features