added looping math to fill in the available empty slots to fill page