Made encode message method generic