added register handler; changed error handling