Refactored to use shared code.