correccion para que sea o(N) - its working but i have no idea why