Added CLI option parser and cleandup some of the code