implement -n option, refactor a bit