factored all code into a fnction