Refactored the Javascript a bit.