fixed DRY problems, refactored program structure