moved code into a function