Fixed bugs pointed out by @jrjohansson.