Solution and tests for issue 387