ugly but working js code