Optimize some code to fix #4

Optimize some code to fix #4