Bunch of bug fixes