Major revisions. Tests pass. I'm lazy. Seems to solve now. Some optimizations maybe needed