add better assert statements and remove specialcasing for WRITE_LOCK
fix assertion
introduce a lock factory wrapper
fix the rest