function is throwing a range error