add mutex-locking and refactoring