Xiangyao fixed a bug in the original code so I'm updating it