Added more error messages, refactoring