working solution for the references, but too slow