Finally seems to be working correctly