Added overarching loop and bug fixes