added event handelrs and abstracted some functionality