There was a couple of typos in the routine. This code will be used by the AI wake feature. Also added a test for the matrix code to make sure they will be no regression.