Introduced mutex to avoid race condition.
Removed unused includes.