refactoring fw_handler.py code