Refactored the js a bit