Using atomic.CompareAndSwap will make this func little cleaner and will get rid of all the lock/unlock calls.