Improved quality, bug fixes, typos. Added test case