Complete Refactoring

* Wrap everything into singleton objects
* Do not use global vars
* Cleanup code