Initial implementation of partially working event system
Minor refactoring and bug fixes