fixes #597 and tested on macosx and Linux