Fixed threading and added a single-threaded version