Factored the exit trap into a function.

Change-Id: Ie97da247441082e753024c3183465c1d62c05f24