Better lifecycle unit tests

Also, refactoring some DB calls for easier testing.