improve performance and refactor (fix #6)