Finished code, sort of works. Time to debug.