Refactor and merge different implementations