Refactor with flock(2)