reduced memory usage. fix bugs.