Move logic to general base class and remove duplicate code.