Added tests.
Corrected logic to pass tests.