Refactor backup, add new utility functions.

And write more tests!