General fixes, improvements and refactoring.

Also numeric work.