major rewrite after reading 'Javascript: the Good Parts'