ugly but working code