Initial refactoring - functions