Replace magic number with constant.
Replace loops with whiles.
Use if let and while let to clean up code.