Fixed a few issues with locking and writing.