fix concurrency issue and minor performance improvement and cleanup