rearanged code to be all inside one function