Small bugfix and refactoring