Rewritten a faster and simplified output printing