Refactored based on Milo's feedback. Added few test cases.