Updated and valgrided code