Refactored according to @githuesch's suggestions