Merged (some of) Miguels changes.