Added new testcases and refactored the code for reverse