error handling and remove unused stuff