reduce complexity and memory usage in main()