Refactor Main Source File

Just a quick clean up, removing some redundancies and extracting concerns into their own methods. Still more can be done, but this is a start.
Yeah, I moved the cast to the `format_messages()` function so all the IDs are ints now.