Improved algorithm for electromagnetic scattering of plane waves and shaped beams by multilayered spheres