refactored and added error handeling