refactor and improve performance