Improve API to handle duplicate words, clean up javascript