How to find peaks & eliminate flat tops

Is it possible for anyone to look through my code and fix any imperfections, I'm stumped..