Refactor

Most importantly, this decouples functions from how they're exposed.

Otherwise just readability improvements.