Solution and tests for issue 387
Merge pull request #393 from lucasmaj/master

Solution and tests for issue 387