Ugly Javascript code