Implemented better structure and event handler