TRQ-2464 and TRQ-2463. Valgrind invalid Read

Fixed code to eliminate valgrind invalid read issues.