adapted from earlier code, working version used to create figure