Issue #1878: Fix the first issue described.
Fix the rsst of #1878
Issue #1878: Add tests