changed whitespace, changed script structure to be more modular and testable. In untested state.