Trying to come up with a JavaScript version that doesn't suck.