Added test support and some refactoring.