Significant refactoring of flanbot.py