fully working code: