Align the implementation with upstream Rails

This is getting really hard to maintain as is.