Clean up locking code a bit