Use the same stats calculation as the API

Also use node-style callbacks, so we're error-aware.

This, and many other changes, should address #4.

/cc @hampelm