Fix issues #22 and #40