Algorithm enhancement and threadsafeness

Enhanced the Algorithm class and tried to make it thread safe.