Fixed bugs pointed out by @blha303