Fixed output bug in algorithm