Added further test cases. Reformatted and optimized code.