Add serialize test and fix bugs