fixed race condition
added active lock, fixed typo
Added comments