Moved javascript out into functions.