Bug fix #9 and #10

In #10 there is a performance issue, see #61