Length issues and a bunch of Valgrind issues fixed

Signed-off-by: Claudio Matsuoka <cmatsuoka@gmail.com>