Maximizing Work and Minimizing Irreversibility in Energy Systems