Apply Eli's changes to the rails4 branch