Attempting to make this less shit with some lame optimisations