adapting it as per @stof comments