more bugfix and optimization.