refactor code & fix bug