working implementation, needs refactor