Refactoring as suggested by @calo81