forked from tbranch. lots of good stuff