it's done!

the error was just a minor logical error. The code I posted above is working now.