Some refactoring and fail fixes