fix error & refactor code