Refactor chatting application code