Maybe all the attribute checking stuff should go in its own function?