improved sendMessage function in background script.