Completed the rewrite.

It should have all the basic features. The code's still a little ugly, though.