Splitting function into smaller ones.
Logging improvements.
Refactoring, more logging.