work-in-progress some fixes and refactoring