rearranging code for optimization and multi-generartions