Fix gaps_beneath/gaps_beneath and do some profiling/optimization;