big TypeScript refactoring