Greatly refactored, lots of code changes