Refactoring: split tools.cpp to multiple files
Fix variable scope