Make sure rest_terminate is always called

Also improves error reporting.