lots of cleanup and refactor