More cleanup, finer-grained locking in a few spots.