code optimized and bug fixed