Added locking to fix #135