Fix a couple of bugs with the help of Mr.Tanov.