Updated with Vinh's modifications.