Grabbed most of the code from rubygems.org's hostess.