Replace debug with events and general cleanup