improved the not working code