added option to redirect output to a file