Large rewrite of the compiler

I'm sorry for this huge commit, but I got carried away.