Reorder while conditions

To allow short-circuit and prevent index-out-of-bounds exception.