Code refactor to improve readability and reuse