Refactored to reduce function length