fix for #673, #706

Thanks @JanRR for analysing and @vores8 for providing the fix