Refactored to reduce code.