Trying to minimilize some if statements, fixed a potential bug as well.