cleaned up loop

does things in steps instead of all in one big printf, to try to make it easier to know what went wrong, but hopefully I fixes the printf issue as well.