Solution without using globals.