Fixed the bug I found. Not tested, should work