Bugs fixed, code design and doc updated