Fixed a few logic errors and now things seem to work.