fixed serious flaw in logic and refactored some bits
lots of stuff refactored and simplified