Rebase to master PR #1437 for fixing #1163

Please review. :)

Tests seems that works.