add terminate() to the end of the function