almost everything works. still not freeing all memory