refactor to speed up class handling