Fixed the bugs

Can't believe I spent 30 minutes debugging this.