refactor code, fix issues