Fix #978 and #1037