[fix] Refactoring started by moving some code to a helper class.