refactored Event detection and improved vectorization