rewrite to use classes and events