cleanup and improved onset detection