All of this if logic can be moved outside of the loop, no need to do it every time.