refractored the gap checking method