refactor and bugfix