Backported some of Fabrizio's changes...