Modularize chat code

This breaks up the code for chat interface into small manageable parts.