Put writing outputs into separate subroutine