More efficient code

Took out 3 if/elseif branches, and fixed a printout count-by-one error.