updated and seems a bit more stable