Fixed serialize function