Cleanup backported from rails4 branch