refactor main_hook to a function