Near-total rewrite

Switched over to using functions for basically everything.  Basically everything works much better now.