optimized code to smaller functions for reusability