rearrange things so that output is only written in one place