some tricky refactoring fixes