first bug-free version, now needs further test
shared_ptr bug fixed.