Refactored to simplify design of code.