finished integrating everything into a single function