Added a relationship for testing.
Refactoring for testability.
Added debug mode and some function.