modified to work with more than 1000 classes