fix issue #4 and add regression tests for it