Refactored writing to log