Some refactoring for generic use.