More work, fixing memleaks and behavior