added lock in close and write