bugfix and optimize