Refactor and improve the filterValues method