performance fix for insert function