Slightly more efficient gap calculation