Cleaned up JavaScript

- wrapped all the code in an onload function
- renamed some functions