Fix #780 and fix #1041