reorder code and changed variable names for simpler read through.