Add functional tests and fix a few problems found while adding them.
Remove unused files and clean up slightly.