Fix logic causing crash