Fixed up some numpy dimensional issues using keepdims argument to np.sum etc.
Cleaned up dimension manipulation