Added boundary checking.