modified version printing algorithm