Partial rewrite of the attributes code

There's more that I'd like to do, but this can land already.