Rationalising return codes and refactoring
Adding tests