cleaned up code

Major refactor - may not compile yet