Refactored javascript to remove varargs, avoid Method.invoke