Improved logging and added file locking