fixes #1615

I rebased @evildmp's pull request so that it only contained the fixes that pertained to it.