Merged Diogo's modifications