Fixed a small bug and a big logical flaw. Seems to be working almost perfectly.