Minor refactoring. Basic error handling.