fixed programmatic usage example and added some cleanup

fixed programmatic usage example and added some cleanup