Moved logging to writing if block