rewritten to TypeScript