Changed locking function and added test