Bug fix for reading from argv and some refactoring