Send error messages to stderr and return non zero value

Refactor out code