Fixed some logic flaws. Something still not right though, needs more debugging