Fixed bug in promise handling
Added tests