refactor code, bugfix