Fixed various bugs in serializing, and added an example for testing.