Added more sophisticated target priority.
Refactoring and file structure changes.