added locking and refactored some public functions with new private functions
merged writeprevent branch using mutex locks