Fix output algorithm