Major refactoring -- needs lots of cleanup