refactor of alphabeta function