fixed trunk-3483 and added unit tests

Hi Daniel,
Can you pls check whether this PR is correct and merge it if so.
Thanks!
Milinda