updating function done, may need efficiency tweaking