Added a service factory test and fixed a few related bugs.