Updated peak detection code