changed back to separated exit handlers and fix foreach warning