working version. A and B are now exchanged. Everything is col major