Fixes some refactoring issues. Documentation and unit testing left.