change error handling. move declarations to header