refactor. comments.
optimize alloc+clear. add a output dump option.