simplify boundary check for layout context
revice loop a little