Add messages.py and re-organized code