Some minor changes and error handling for log writing