Unused code removed. Some minor changes and refactoring