add forked and modified gist from hbrunn