fix for #1345 and partial fix for #1310 #1240