change locking mechanics when writing
refactor wait for file to become available