Fix bug spotted by valgrind
Fix few bugs in error reporting