added some more function definitions, touched up main structure