Moved writing to output into method