code refactor and modularization