Fixed some termination conditions