Local method lock

Replaces module-wide locking code with more precise local-method lock.

This changes the API slightly, but shouldn't affect the way anyone was using it.