Refactor del main handler