fixes #664 refactor this to be a little nicer