Implemented some unit tests. Tweaked GUI and refactored code.

you might want to use thread-local storage for this. then your tests can run in parallel too...