fixed bugs, wrote a js version