implement history and refactor messages and threads