Huge rework, much neater code, does not currently run
merge