maybe more efficient function
fixing references