Refactoring functions. Consolidated some of the common functions