fix performance problem when calculating the level