this code is starting to look a little ugly... I'll try to find a prettier way. It really is time for bed now