Fixed few bugs, made some serialization refactoring