bugfix and refactoring
has bug