added a range check

may hang otherwise, see 7e35aa2