Creating Helper functions and refactoring previous code