refactor, some bug fixes
merge