Locking added for multithreading issues