Messy but basically working!