My function, some test code, and a brief explanation of the code.