Fairly complete implementation, with inplace working correctly