more accurate code around offscreen margin