Add handlers to exit more gracefully.

@nanek