properly exit the function