refactored the whole program