Fix some comments and other small things
Use Encoder instead of marshalling and writing manually
Created a generic sendMessage function