cleaner impelementation based onf @FokkeZB's comment