Refactored the code, and added more specifications