This feels very hackish. Why not make the necessary variables global (*percents* should be the one I guess) and split this out in a separate function?