Bug fixed and code cleanup