Code finished for command line use, not tested yet