Refactored into smaller methods.