Fixed matrix bugs