Refactors and modularizes