Improving javascript snippets