refactored the javascript to get smaller methods.