getting the sorted + more logic in the patch function from stable/folsom