Refactored, cleaned up Matrix code.