Add boundary checking