would love to see a more pythonic rewrite of this..