split big method into smaller unitTestable methods. Some code cleanups