better end function