simplified code, easy modification for different max_kernel sizes