Various fixes and improvements, ready for test implementation.