Fix #416 and partially #417

How I fixed it so far.