bug fixed, refactor code