Cleaned up the code. Added alternative set_matrix function.