Error catching and bug fixes