refactored to consolidate code