Improvement WIP, abandoned in favour of a much cleaner and improved version that I'll probably never start.