Merged Gaya's pull request, plus some additions