Added checking for boundaries