Added option to read from stdin and refactored some old code