added proper handling of ranges when storing references