Fix locking issues

Includes fix for #49