Refactor messagelist functions

See line notes for more info.