starting to refactor database and events - half way through. Almost certainly broken