rearranged code in message handler

and created new function getUrl