If this function stays this complex it will need more inline comments to walk a reader through what it's doing and why