Implement serialize, and some parameter passing optimization