Added error check for empty messages list.