Restructured the onMessage command completely to be neater