Was comparing loop variable incorrectly