Improvements

Various fixes, test cases and improvements.