JS efficiency and cleanup

Less repeated code FTW.