optimize locking to move file access out of critical section