BS-7589
Fix stacktrace, improve code quality