abstract the code into a function