bug fix for #22 + several others