added exit conditions