bug fixes and test improvement