small reorder of gap and selection related code