Updated to nearly working code.