Improve code reusability

and format for consistency.