Finalyzing integration with aneesh code. Changes made locally for his code to work in mine