Refactor and simplify implementation