Added input format and refactored the code