this fixes #246, #303 (more testing needed)