Changed code to be event based and fixed history