Undid unnecessary slice changes and added error handling.