Tests and cleanup

A bunch of refactoring and some basic metagraph API testing.