Refactoring to use more functions and reduce footprint