optimized freak.cpp