fixes #407, function works but need more general function