updates to get this working with newer versions, and some refactoring