fixed memory leak, code cleanup, more concise output