refactoring a little bit and adding file lock