Cleanups, logic checks and insert function for rec.c