Ported exit handler to C