added TERMINATE logic