Changed the structure to put the gathering into a function