massive refactoring

moving to the new Arrangement module slowly.
also breaking up the huge main fn into many small fns,
with simple records to hold different parts of state