incorporating Andi's patch