Refactoring + bugfix