fix error and improve performance