working code, still some changes needed