Refactored the JavaScript a bit