GIBS-466 Merged code with added error handling and return codes