improved but still not working code