Refactored everything to make it duo-thread friendly
Added some instrumentation.