Refactored. Multithreaded, all be it not neatly.