factor this into a util function
use apply_metaclass in more places