Refactored a lot.