add check fro end of mem

this stops the search once end of mem is checked