changed order of arguments
changed back to separated exit handlers and fix foreach warning