The fact that you have to link to StackOverflow to explain this code, means that it should probably be re-written to be simpler.