Updated file taking into account Shirren's comments and making the code simpler