code cleanup, fixed memory leaks, ansi color handling