Wrong placement of the cleanup functions