Fix bug found by coverity scan. Hopefully I didn't break anything