Remove useless nested functions and unused functions