some refactoring based on feedback from @tobes