Cleanup on error in the main loop

Signed-off-by: John Else <john.else@citrix.com>