Fixed code

Okay, I merged Samed's code with my own, then I am pushing my changes
from today (because that code was overwritten)

Let me know if anything is broken. This definitely needs to be fixed. I
may do a deeper line-by-line analysis tomorrow.

Thank goodness GitHub does that kind of stuff.