Concurrency issue fix as well as code refactoring