Added matrix bounds checking. Changed transpose function to set correct output rows and  columns.