motdified to efficient code