Added valgrind check and added comments