Fix Bugs

Fix bugs and optimize codes.