Move message logic into JS