Minor fixes, refactoring and improved test cases