improved implementation of print() function