optimized classification routine