Code optimized and bug fixed