Fix #1365 and #1364, some other tweaks