Fixed concurrency issues; refactored