code cleanup, memory-leak fix - BROKEN