Rewrote crucial loops. All bugs fixed.