third attempt - algorithm has been fleshed out but needs to be implemented