This doesn't look very pythonic? Isn't there a better way to do this?