Shortened the code and edited the comments