Rearranged ProcessMessage function flow for clarity and less nesting.