Moved actual insertion code to its own function