Added extensive test coverage and better error messages.