Refactored rectangle processing