My concern was that the remainder in the highest element of r may not be exactly 0. It looks mathematically correct.