Code reorganized into a more Pythonic structure.