It works now, but there are a lot of magic number in the program