Cleanup code and fix bugs, made Segments serializable