fixing bugs and adding centralized error handling