refactor and minor bugfix