Updated implementation of insert method