code refactored and program almost finished