Code cleanup and fixes for JS errors.