Improved file handling for the lockfile method.