Updated the code more with Kin's help.