Limit lock scope to prevent recursive locking

Fixes #52