refactoring and a small fix for frequency analysis, its even narrow than before