Final working code.