Refactored 27. Added test cases. Fixed bug.