Bugs fixing and code optimizing