Performance and multi-threading

Lots of refactoring, cleaning up bad code. Fixed a few bugs.
Classification is much much faster.