Fixed for bugs found via Coverity