better code structure, and minimal refactor