Ugly but appears to work