Fix logical error causing crash