Refactor and minimize code