free memory in creg test / remove duplicate code