translated in javascript by sam and martin