added argument parsing and cleaned up code