convert a lot of JS to proper Dart (but nothing working yet)