Fix logic flow errors