This still needs cleaned up (as well as other functions in commands.c).