This is the most insane code I have ever written (AND it works EM). I have little hope of a proper explaination of it