Lots of cleanups, check all values, got rid of mallocs, added better messages.