Code runs, but issue with energy calculation