refactored and beautified the code, removed evil hacks