added a range check for x and y values