fix stack trace and add tests