top and bottom detection works properly