clean up, some refactoring, more documentation