Refactored to take care of both questions.