fixed mallocs and added insert