makes several improvements, cleanups and refactorings