Condensed code, added comments. Added test cases and testing script