Added a lot of bug fixes