Added command line options and refactored a bit