added some multi-threading and error logging