Moved the more verbose parts of the routing code into subfunctions and arranged it so that more relevant parts are closer to each other. Also added the SQL statement that is being delayed to the message.