refactored to clean up passing a zillion parameters to function, now variables hash is passed instead
minor reformatting