Rewrote the code, much cleaner, actually working now.