refactor writing to results to remove redundant code