Added tests for serialization, and fixed some bugs.