Fixed matrix out of bounds errors, needs to be implemented and checked there