Major refactoring to better organize things.