Beginning of last major rewrite (hopefully)