Copy of Bella's solution with a few minor modifications to use global variables to have both accessible rather then either or and not need to be called each run through.