Fix #537 and #581