Bugs fixed and code design