Proper implementation of the perak detector and beat duration window. Cleanup and comments