Bulk of rafactoring changes/fixes

Huge number of changes, see commit messages.