Bug fixes and error catching