Refactor js

Rewriting our js to be more readable and less hacky.


Also this should occur in beforeSend: