Andreas suggested changes

Some suggestions for refactoring the code.