patched version from jlongster's github. ergh.