bug fixed and optimizations