Version that works. Room for improvement... but it works