Improve performance and fix #16