Update 2: Everything except explanation of code