actual fix for #346, accidently pushed broken code in previous commit