Added comments, improved script flow, added main() function to hold meaty js code