Refactoring template models and lib.send_messages