Massively refactored after meeting with Alec.