Reapplying fix #396

This has been reverted in #502 although it wasn't the reason for the problem.