Significant rewrite

Pretty happy with how this turned out, but it's still an awful lot of LOC