Start using the Python logging module and tidied things up a bit.