added a validation logic
made error handling better