Refactoring, included demo code.