reworked algorithm for 5x5