First version of code. Not everything commented. Nothing tested.