Applied code from #477

Fixes #477