Fixed bug 5502 + document + test

I included most of the feedback I received on the previous pull requests. (#159 and #160) Unfortunately, I could not fix the previous branch, so I had to do it again from scratch. Please remove the other two pull requests I did earlier!