fixed locking issues. passes tests 1-11, but test 6 is still somewhat flaky due to race conditions