added boundary checks to the box