Fixed index out of bounds bugs. TODO: Fix randomly occuring SegFaults