Fixed for/if nesting bug