We propose a novel spectral filter optimization algorithm for the single trial ElectroEncephaloGraphy (EEG) classification problem. The algorithm is designed to improve the classification accuracy of Common Spatial Pattern (CSP) based classifiers. The algorithm is based on a simple statistical criterion, and allows the user to incorporate any prior information one has about the spectrum of the signal. We show that with a different preprocessing, how a prior knowledge can drastically improve the classification or only be misleading. We also show a generalization of the CSP algorithm so that the CSP spatial projection can be recalculated after the optimization of the spectral filter. This leads to an iterative procedure of spectral and spatial filter update that further improves the classification accuracy, not only by imposing a spectral filter but also by choosing a better spatial projection.