fixed a nasty bug I somehow created by reverting and rewriting the code