Fix promise and error handling & add test.