added more range checks