added kml output and proper cleanup