closing the function properly