Basics work! Also, large code cleanup.

Module structure is far nicer than having one massive frankenfunction.

Next todo: typevar substitution, SCC detection, optimisations