Added try catch to logging and improved readability