Bugfixes courtesy of Dread Quixadhal <quixadhal@gmail.com>