Reformatted code with jsbeautifier and fixed a few small errors