Refactor and fix #5