fixed quite a lot of bugs