Added halting condition + exception handling