Refactored, added error passing