Optimised version of getRelatedClasses