Add mutex locking to more functions.

Fixes #15