code reshuffled, commented